<template>
  <view>
    <view class="form-container">
      <uni-forms ref="form" :modelValue="formData" :rules="rules" labelPosition="top">
        <cu-cell-group>
          <uni-forms-item name="meetingTime">
            <cu-cell
              title="会议时间"
              required
              link
              placeholder="选择时间"
              :description="formData.meetingTime"
              @click="showMeetingTimePicker"
            ></cu-cell>
          </uni-forms-item>
          <uni-forms-item name="itemId">
            <cu-cell
              title="排班事项"
              required
              link
              placeholder="选择排班事项"
              :description="formData.itemName"
              @click="showShiftItemSelect"
            ></cu-cell>
          </uni-forms-item>
          <uni-forms-item name="itemClassId">
            <cu-cell
              title="班次"
              required
              link
              placeholder="选择班次"
              :description="formData.itemClassName"
              @click="showShiftClassSelect"
            ></cu-cell>
          </uni-forms-item>
          <uni-forms-item name="ahChargeUserId">
            <cu-cell
              title="安全主办"
              required
              link
              placeholder="选择人员"
              :description="formData.ahChargeUserName"
              @click="showUserSelect('ahChargeUser')"
            ></cu-cell>
          </uni-forms-item>
          <uni-forms-item name="zhChargeUserId">
            <cu-cell
              title="综合主办"
              required
              link
              placeholder="选择人员"
              :description="formData.zhChargeUserName"
              @click="showUserSelect('zhChargeUser')"
            ></cu-cell>
          </uni-forms-item>
          <uni-forms-item name="scChargeUserId">
            <cu-cell
              title="生产主办"
              required
              link
              placeholder="选择人员"
              :description="formData.scChargeUserName"
              @click="showUserSelect('scChargeUser')"
            ></cu-cell>
          </uni-forms-item>
          <uni-forms-item name="sbChargeUserId">
            <cu-cell
              title="设备主办"
              required
              link
              placeholder="选择人员"
              :description="formData.sbChargeUserName"
              @click="showUserSelect('sbChargeUser')"
            ></cu-cell>
          </uni-forms-item>
          <uni-forms-item name="attendUserList">
            <CuUserSelector
              label="参会人员"
              placeholder="选择人员"
              v-model="formData.attendUserList"
              required
              multiple
              :deptId="deptId"
            />
          </uni-forms-item>
          <uni-forms-item name="">
            <CuUserSelector label="参会劳务班组" placeholder="选择人员" v-model="formData.laborTeamUserList" multiple />
          </uni-forms-item>
          <uni-forms-item name="planAttendLeaderList">
            <CuUserSelector label="参会领导" placeholder="选择人员" v-model="formData.planAttendLeaderList" multiple />
          </uni-forms-item>
          <uni-forms-item name="compereUserId">
            <cu-cell
              title="主持人"
              required
              link
              placeholder="选择人员"
              :description="formData.compereUserName"
              @click="showUserSelect('compereUser')"
            ></cu-cell>
          </uni-forms-item>
          <uni-forms-item name="registrarUserId">
            <cu-cell
              title="记录人"
              required
              link
              placeholder="选择人员"
              :description="formData.registrarUserName"
              @click="showUserSelect('registrarUser')"
            ></cu-cell>
          </uni-forms-item>
        </cu-cell-group>
        <view class="background-white bor-top">
          <uni-forms-item name="requireSummary" label="班长总结" :label-width="100" required>
            <textarea
              v-model="formData.requireSummary"
              class="text-area"
              placeholder="请输入"
              :maxlength="1000"
              style="overflow: scroll"
            />
          </uni-forms-item>
        </view>
        <view class="background-white bor-top">
          <uni-forms-item name="shiftContent" label="交班情况" :label-width="100" required>
            <textarea
              v-model="formData.shiftContent"
              class="text-area"
              placeholder="请输入"
              :maxlength="1000"
              style="overflow: scroll"
            />
          </uni-forms-item>
        </view>
        <view class="submit" @click="handleSubmit">完成</view>
      </uni-forms>
    </view>
    <CuDateTimePicker ref="meegtingTimePicker" @change="meetingTimePickerChange" />
    <shiftItemSelect ref="shiftItemSelectRef" @confirm="handleShiftItemSelectConfirm" />
    <shiftClassSelect ref="shiftClassSelectRef" @confirm="handleShiftClassSelectConfirm" />
    <userSelector ref="userSelectorRef" :dept-id="deptId" @change="handleUserSelectorChange" />
  </view>
</template>

<script>
import CuCellGroup from '@/components/cu-cell/cu-cell-group.vue'
import CuCell from '@/components/cu-cell/cu-cell.vue'
import CuDateTimePicker from '@/components/cu-date-time-picker/cu-date-time-picker.vue'
import shiftItemSelect from './components/shift-item-select.vue'
import shiftClassSelect from './components/shift-class-select.vue'
import { addMeeting, getDutyUser, getLastAdvice } from '@/api/duty-basic/duty-meeting'
import CuUserSelector from '@/components/CuUserSelector/index.vue'
import userSelector from '@/components/user-selector/index.vue'
import dayjs from 'dayjs'
import store from '@/store'

export default {
  name: 'AddSafetyMeeting',
  data() {
    return {
      deptId: store.getters.deptId,
      formData: {
        meetingTime: undefined,
        itemId: undefined,
        itemName: undefined,
        itemClassId: undefined,
        itemClassName: undefined,
        itemDate: '', // 班次时间
        zhChargeUserId: '',
        zhChargeUserName: '',
        ahChargeUserId: '',
        ahChargeUserName: '',
        scChargeUserId: '',
        scChargeUserName: '',
        sbChargeUserId: '',
        sbChargeUserName: '',
        requireSummary: '',
        attendUserList: [], //参会人员
        laborTeamUserList: [], //参会劳务班组
        planAttendLeaderList: [], //参会领导
        compereUserId: '', //主持人
        compereUserName: '',
        registrarUserId: '', //记录人
        registrarUserName: '',
        shiftContent: '' //交班情况
      },
      rules: {
        meetingTime: {
          rules: [
            {
              required: true,
              errorMessage: '会议时间不能为空'
            }
          ]
        },
        itemId: {
          rules: [
            {
              required: true,
              errorMessage: '排班事项不能为空'
            }
          ]
        },
        itemClassId: {
          rules: [
            {
              required: true,
              errorMessage: '班次不能为空'
            }
          ]
        },
        ahChargeUserId: {
          rules: [
            {
              required: true,
              errorMessage: '安全主办不能为空'
            }
          ]
        },
        zhChargeUserId: {
          rules: [
            {
              required: true,
              errorMessage: '综合主办不能为空'
            }
          ]
        },
        scChargeUserId: {
          rules: [
            {
              required: true,
              errorMessage: '生产主办不能为空'
            }
          ]
        },
        sbChargeUserId: {
          rules: [
            {
              required: true,
              errorMessage: '设备主办不能为空'
            }
          ]
        },
        attendUserList: {
          rules: [
            {
              required: true,
              errorMessage: '参会人员不能为空'
            }
          ]
        },
        requireSummary: {
          rules: [
            {
              required: true,
              errorMessage: '工作要求不能为空'
            }
          ]
        },
        compereUserId: {
          rules: [
            {
              required: true,
              errorMessage: '主持人不能为空'
            }
          ]
        },
        registrarUserId: {
          rules: [
            {
              required: true,
              errorMessage: '记录人不能为空'
            }
          ]
        },
        shiftContent: {
          rules: [
            {
              required: true,
              errorMessage: '交班情况不能为空'
            }
          ]
        }
      },
      userSelectorType: ''
    }
  },
  provide: {
    isAll: 'Cu'
  },
  methods: {
    showMeetingTimePicker() {
      this.$refs.meegtingTimePicker.show()
    },
    meetingTimePickerChange(value) {
      if (this.formData.meetingTime == value) return
      this.formData.meetingTime = value
      this.formData.itemId = ''
      this.formData.itemName = ''
      this.formData.itemClassId = ''
      this.formData.itemClassName = ''
      this.formData.itemDate = ''
      this.resetFormData()
    },
    showShiftItemSelect() {
      if (!this.formData.meetingTime) {
        uni.showToast({
          icon: 'none',
          title: '请先选择会议时间'
        })
        return
      }
      this.$refs.shiftItemSelectRef.query(this.formData.meetingTime)
      this.$refs.shiftItemSelectRef.open()
    },
    handleShiftItemSelectConfirm({ value, name }) {
      if (this.formData.itemId === value) return
      this.formData.itemId = value
      this.formData.itemName = name
      this.formData.itemClassId = ''
      this.formData.itemClassName = ''
      this.formData.itemDate = ''
      this.resetFormData()
    },
    showShiftClassSelect() {
      const { meetingTime, itemId } = this.formData
      if (!itemId) {
        uni.showToast({
          icon: 'none',
          title: '请先选择排班事项'
        })
        return
      }
      this.$refs.shiftClassSelectRef.query({ meetingTime, itemId })
      this.$refs.shiftClassSelectRef.open()
    },
    handleShiftClassSelectConfirm({ value, name, teamDate }) {
      if (this.formData.itemDate === teamDate) return
      this.formData.itemClassId = value
      this.formData.itemClassName = name
      this.formData.itemDate = teamDate
      this.resetFormData()
      this.getDutyUserList()
      this.getLastShiftContent()
    },
    showUserSelect(type) {
      this.userSelectorType = type
      this.$refs.userSelectorRef.open()
    },
    handleUserSelectorChange({ value, name }) {
      this.formData[`${this.userSelectorType}Id`] = value
      this.formData[`${this.userSelectorType}Name`] = name
      this.userSelectorType = ''
    },
    // 获取职能
    async getDutyUserList() {
      const { itemDate, itemId, itemClassId } = this.formData
      if (!itemClassId) {
        uni.showToast({
          icon: 'none',
          title: '请先选择班次'
        })
        return
      }
      const params = {
        teamDate: itemDate,
        itemId,
        classId: itemClassId
      }
      const { data } = await getDutyUser(params)
      const { userId: ahChargeUserId, userName: ahChargeUserName } =
        data.filter((item) => item.dutyName === '安全主办')?.[0] || {}
      this.formData.ahChargeUserId = ahChargeUserId
      this.formData.ahChargeUserName = ahChargeUserName
      const { userId: zhChargeUserId, userName: zhChargeUserName } =
        data.filter((item) => item.dutyName === '综合主办')?.[0] || {}
      this.formData.zhChargeUserId = zhChargeUserId
      this.formData.zhChargeUserName = zhChargeUserName
      const { userId: scChargeUserId, userName: scChargeUserName } =
        data.filter((item) => item.dutyName === '生产主办')?.[0] || {}
      this.formData.scChargeUserId = scChargeUserId
      this.formData.scChargeUserName = scChargeUserName
      const { userId: sbChargeUserId, userName: sbChargeUserName } =
        data.filter((item) => item.dutyName === '设备主办')?.[0] || {}
      this.formData.sbChargeUserId = sbChargeUserId
      this.formData.sbChargeUserName = sbChargeUserName
    },
    async getLastShiftContent() {
      const { itemId, itemClassId, itemDate } = this.formData
      const params = {
        teamDate: itemDate,
        itemId,
        itemClassId
      }
      this.formData.shiftContent = (await getLastAdvice(params))?.data || '正常'
    },
    resetFormData() {
      this.formData.ahChargeUserId = ''
      this.formData.ahChargeUserName = ''
      this.formData.zhChargeUserId = ''
      this.formData.zhChargeUserName = ''
      this.formData.scChargeUserId = ''
      this.formData.scChargeUserName = ''
      this.formData.sbChargeUserId = ''
      this.formData.sbChargeUserName = ''
      this.formData.shiftContent = ''
    },
    async handleSubmit() {
      await this.$refs.form.validate()
      uni.showModal({
        content: '提交后，填写的内容将不可以修改，请确定填写无误后再提交',
        confirmText: '确定',
        success: async (res) => {
          if (res.confirm) {
            await addMeeting({
              ...this.formData,
              meetingTime: dayjs(this.formData.meetingTime).format('YYYY-MM-DD HH:mm')
            })
            uni.showToast({
              title: '提交成功！',
              icon: 'success'
            })
            uni.navigateBack()
          }
        }
      })
    }
  },
  onReady() {
    // 需要在onReady中设置规则
    this.$refs.form.setRules(this.rules)
  },
  components: {
    CuCellGroup,
    CuCell,
    CuDateTimePicker,
    shiftItemSelect,
    shiftClassSelect,
    CuUserSelector,
    userSelector
  }
}
</script>

<style lang="scss" scoped>
.submit {
  width: 600rpx;
  height: 88rpx;
  background: #454a5e;
  border-radius: 50rpx 50rpx 50rpx 50rpx;
  opacity: 1;
  text-align: center;
  line-height: 88rpx;
  font-size: 36rpx;
  font-weight: 400;
  color: #ffffff;
  margin: 0 auto;
  margin-top: 70rpx;
  margin-bottom: 70rpx;
}

.text-area {
  height: 300rpx;
}
</style>
